import Axios from 'axios'
import router from '../router'

if (window.localStorage.getItem('token')) {
  Axios.defaults.headers.common['Authorization'] = window.localStorage.getItem('token')
}

export let instance = Axios.create({
  /* 设置请求头 */
  headers: {
    // 'Content-Type': 'application/x-www-form-urlencoded'
      'Content-Type': 'application/json;charset=UTF-8'
  },
  /* 设置baseURL */
  baseURL: process.env.NODE_ENV === 'production' ? 'http://simple/api' : 'http://localhost:8080'
})
/* respone拦截器 */
instance.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if (error.response) { // //请求已发出，服务器使用状态码进行响应
      switch (error.response.status) {
        case 401:
          router.replace({
            path: 'Login',
            query: { redirect: router.currentRoute.fullPath } // 将跳转的路由path作为参数，登录成功后跳转到该路由
          })
        case 404:
          router.replace ({
            path: 'NotFound'
          })
      }
    }
    return Promise.reject(error.response)
  }
)

export const login = ({ loginUser, loginPassword }) => {
  return instance.post('/account/login', {
    appId: loginUser,
    password: loginPassword,
    role: 'ADMIN'
  })
}

/* API */
export const getApiListPage = params => {
  return instance.get('/resource/api', {
    params: params
  })
}

export const addApi = params => {
  return instance.post('/resource/api', {
    params: params
  })
}

export const editApi = params => {
  return instance.put('/resource/api', {
    params
  })
}

export const removeApi = params => {
  return instance.delete('/resource/api', {
    params: params
  })
}

/* Role */
export const getRoleListPage = params => {
  let url = '/role/' + params.currentPage + '/' + params.pageSize
  return instance.get(url)
}

export const addRole = params => {
  return instance.post('/role', {
    name: params.name,
    code: params.code,
    status: params.status
  })
}

export const editRole = params => {
  return instance.put('/role', {
    id: params.id,
    name: params.name,
    code: params.code,
    status: params.status
  })
}

export const removeRole = params => {
  let url = '/role/' + params.roleId
  return instance.delete(url)
}

export const getAuthResPage = params => {
  let url = '/role/api/' + params.roleId + '/' + params.currentPage + '/' + params.pageSize
  return instance.get(url)
}

export const getRestApiPage = params => {
  let url = '/role/api/-/' + params.roleId + '/' + params.currentPage + '/' + params.pageSize
  return instance.get(url)
}

export const addAuthRes = params => {
  return instance.post('/role/authority/resource', {
    roleId: params.roleId,
    resourceId: params.resourceId
  })
}

export const removeAuthRes = params => {
  let url = '/role/authority/resource' + '/' + params.roleId + '/' + params.resourceId
  return instance.delete(url)
}

/* USER */
export const getUserListPage = params => {
  return instance.get('/user/list', {
    params: params
  })
}

export const addMember = params => {
  return instance.post('/user/member', {
    params: params
  })
}

export const removeMember = params => {
  let url = '/user/member/' + params.userId
  return instance.delete(url)
}

export const editUser = params => {
  return instance.put('/user/update', {
    params: params
  })
}

export const addSupplier = params => {
  return instance.post('/user/supplier', {
    params: params
  })
}

export const removeSupplier = params => {
  let url = '/user/supplier/' + params.userId
  return instance.delete(url)
}

export const getSupplierMsg = params => {
  let url = '/user/supplier/' + params.userId
  return instance.get(url)
}

export const addAdmin = params => {
  return instance.post('/user/admin', {
    params: params
  })
}

export const removeAdmin = params => {
  let url = '/user/admin/' + params.userId
  return instance.delete(url)
}

/* 商品管理 */
export const getGoodsListPage = params => {
  return instance.get('/common/goods', {
    params: params
  })
}

export const getSuppilerOptions = () => {
  return instance.get('/goods/supplier')
}

export const getCityOptions = () => {
  return instance.get('/goods/city')
}

export const getCategoryOptions = () => {
  return instance.get('/goods/category')
}

export const addGoods = params => {
  return instance.post('/goods', {
    params: params
  })
}

export const updateGoods = params => {
  return instance.put('/goods', {
    params: params
  })
}

export const removeGoods = params => {
  return instance.delete('/goods', {
    params: params
  })
}

export const getGoodsByName = params => {
  return instance.get('/goods', {
    params: params
  })
}

export const getOrderList = params => {
  return instance.get('/order/list', {
    params: params
  })
}
